package nbcp.base.extend


import nbcp.base.comm.SystemContext
import nbcp.db.CodeName
import nbcp.web.HttpContext
import nbcp.db.IdName

import nbcp.base.utils.MyUtil
import nbcp.db.LoginUserModel
import nbcp.db.mongo.*
import nbcp.db.mongo.entity.SysUser
import nbcp.web.ClientIp
import nbcp.web.MyHttpRequestWrapper
import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpSession

/**
 * Created by udi on 17-5-23.
 */
/**
 * 当是代理的时候,只能取出 corpId 的值.
 */


//图片验证码,以后保存到 redis.
var HttpSession.ValidateCode: String
    get() {
        return this.getAttribute("ValidateCode")?.toString() ?: "";
    }
    set(value) {
        this.setAttribute("ValidateCode", value);
    }

//手机验证码,以后保存到 redis.
var HttpSession.MobileCode: String
    get() {
        return this.getAttribute("MobileCode")?.toString() ?: "";
    }
    set(value) {
        this.setAttribute("MobileCode", value);
    }



//只在 Corp 项目中使用
val HttpServletRequest.IsProxy: Boolean
    get() {

        //JWDTK,在 cookie 中的token
        var admin_token = this.getHeader("proxy")
        if (admin_token.HasValue == false) {
            return false;
        }

//        return@lazy true;
        var jwdSession = SystemContext.getSessionData?.invoke(admin_token, "sessionAttr:" + SystemContext.loginSessionKey) as IdName?
        if (jwdSession != null && jwdSession.id.HasValue) {
            return true;
        }

        return false
    }

var HttpServletRequest.Corp: IdName
    get() {
        var corp = this.session.getAttribute("Corp");
        if (corp == null) {
            if( this.IsProxy) {
                return IdName(this.getParameter("Corp"), "");
            }
            else return IdName();
        }
        return corp as IdName
    }
    set(value) {
        this.session.setAttribute("Corp", value);
    }